<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Filament\Facades\Filament;
use App\Models\Order;

class RechargeController extends Controller
{

    protected $payment_method;
    protected $amount;
    protected $order_id;
    public function __construct($payment_method, $amount)
    {
        $this->payment_method = $payment_method;
        $this->amount = $amount;
        $this->order_id = date('YmdHis').Filament::auth()->id().rand(1000,9999);

        if($this->payment_method == "alipay"){
            $this->payUsingAlipay();
        }
    }

    public function payUsingAlipay()
    {
        $this->createOrder();
        return redirect()->route('alipay', ['price' => $this->amount, 'plan_name' => "账号充值", 'order_id' => $this->order_id]);
    }

    public function createOrder() {
        $order = new Order();
        $order->order_id = $this->order_id;
        $order->user_id = Filament::auth()->id();
        $order->payment_method = $this->payment_method;
        $order->amount = $this->amount;
        $order->status = "pending";
        $order->order_type = "balance_recharge";
        $order->remark = "账号充值";
        $order->save();
    }
}
